package com.qf.myblogssm.controller;

import com.qf.myblogssm.pojo.Message;
import com.qf.myblogssm.pojo.Tcomment;
import com.qf.myblogssm.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * create by 王镜 on 2019/6/18 0018 9:12
 */
@Controller
@RequestMapping("/comments")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @RequestMapping("/del")
    @ResponseBody           //转换为json格式
    public Message del(Integer id){
        Message msg=new Message();
        try{
            commentService.del(id);
            msg.setMsg("success");
            return msg;
        }catch (Exception e){
            e.printStackTrace();
            msg.setMsg("操作异常");
            return msg;
        }
    }

    @RequestMapping("/add")
    @ResponseBody
    public Message add(Tcomment tcomment){
        //封装一个全局异常处理类   //类的反射机制      //MySQL的基础 ddl dtl del dml 脏读 幻读 不可重复读 事务 ACID（原子性、一致性、。。）
        Message msg=new Message();
        try{
            commentService.add(tcomment);
            msg.setMsg("success");
            return msg;
        }catch (Exception e){
            e.printStackTrace();
            msg.setMsg("操作繁忙");
            return msg;
        }
    }
    @RequestMapping("/star")
    @ResponseBody
    public Tcomment star(Integer id){
        try{
            commentService.star(id);
            Tcomment tcomment=commentService.getCommentByid(id);
            return tcomment;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Tcomment();
    }
    @RequestMapping("/diss")
    @ResponseBody
    public Tcomment diss(Integer id){
        try{
            commentService.diss(id);
            Tcomment tcomment=commentService.getCommentByid(id);
            return tcomment;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Tcomment();
    }

}
